package smartcase.config.auth;

import com.google.common.collect.ImmutableSet;
import org.springframework.stereotype.Component;
import smartcase.base.auth.*;

import java.util.Collection;

/** @see AuthenticationInterceptor */
@Component
public class AuthMetaImpl implements AuthMeta {

  private AuthCheckConfiguration cfg;

  public AuthMetaImpl(AuthCheckConfiguration cfg) {
    this.cfg = cfg;
  }

  @Override
  public Collection<AuthRegistration> setupAuthRules() {
    return cfg.getAuthGroups().stream()
        .map(
            group ->
                AuthRegistration.builder()
                    .group(group.getGroup())
                    .addAuthPathPattern(group.getAuthPatterns())
                    .addAuthExcludePathPattern(group.getExcludeAuthPatterns())
                    .setupCredentialFunction(
                        new DefaultTokenVerify(group.getTokenFlag(), group.getFlagValue()))
                    .build())
        .collect(ImmutableSet.toImmutableSet());
  }
}
